/*
 *  Copyright 2004 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.webapp.action.core.contentFieldValue;

import com.blandware.atleap.webapp.action.core.BaseAction;
import com.blandware.atleap.webapp.form.core.ProcessSetForm;
import com.blandware.atleap.webapp.util.core.WebappConstants;
import org.apache.commons.validator.GenericValidator;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;

/**
 * <p>Processes set of selected content field values and does something with
 * them all (edit/view/delete)
 * </p>
 * <p><a href="ProcessValueSetAction.java.html"><i>View Source</i></a></p>
 * <p/>
 *
 * @author Sergey Zubtsovskiy <a href="mailto:sergey.zubtsovskiy@blandware.com">&lt;sergey.zubtsovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.17 $ $Date: 2006/08/03 10:07:50 $
 * @struts.action path="/core/contentFieldValue/processValueSet"
 * name="processSetForm"
 * scope="request"
 * validate="false"
 * roles="core-contentFieldValue-list"
 * @struts.action-forward name="callCreateOrUpdateContentFieldValue"
 * path="/core/contentFieldValue/callCreateOrUpdate.do"
 * redirect="false"
 * @struts.action-forward name="viewContentFieldValue"
 * path="/core/contentFieldValue/view.do"
 * redirect="false"
 * @struts.action-forward name="deleteContentFieldValue"
 * path="/core/contentFieldValue/delete.do"
 * redirect="false"
 * @struts.action-forward name="viewContentField"
 * path="/core/contentField/view.do"
 * redirect="true"
 */
public final class ProcessValueSetAction extends BaseAction {

	/**
	 * @param mapping  The ActionMapping used to select this instance
	 * @param form     The optional ActionForm bean for this request (if any)
	 * @param request  The HTTP request we are proceeding
	 * @param response The HTTP response we are creating
	 * @return an ActionForward instance describing where and how
	 *         control should be forwarded, or null if response
	 *         has already been completed
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
	                             HttpServletRequest request, HttpServletResponse response) throws Exception {

		ProcessSetForm processSetForm = (ProcessSetForm) form;
		ArrayList selectedLocalesList = new ArrayList(processSetForm.getCheckedBoxes().keySet());

		Long contentFieldId = null;
		if ( !GenericValidator.isBlankOrNull(request.getParameter("contentFieldId")) ) {
			contentFieldId = Long.valueOf(request.getParameter("contentFieldId"));
		} else {
			if ( log.isWarnEnabled() ) {
				log.warn("Missing content field ID. Returning to index...");
			}
			return mapping.findForward("admin");
		}

		request.getSession().setAttribute(WebappConstants.CONTENT_FIELD_ID_KEY, contentFieldId);

		if ( selectedLocalesList.isEmpty() ) {
			return mapping.findForward("viewContentField");
		}

		request.setAttribute(WebappConstants.CONTENT_FIELD_VALUE_SELECTED_LOCALES_KEY, selectedLocalesList);

		ActionForward forward = null;
		if ( request.getParameter("editSelected") != null ) {
			forward = mapping.findForward("callCreateOrUpdateContentFieldValue");
		} else if ( request.getParameter("viewSelected") != null ) {
			forward = mapping.findForward("viewContentFieldValue");
		} else if ( request.getParameter("deleteSelected") != null ) {
			forward = mapping.findForward("deleteContentFieldValue");
		} else {
			forward = mapping.findForward("viewContentField");
		}

		return forward;
	}
}